<template>
  <com-card>
    <div slot="com_title">商品添加</div>
    <div slot="com_content">
      <el-form label-width="100px" :model="formObj">
        <el-form-item label="商品名称">
          <el-input v-model="formObj.name"></el-input>
        </el-form-item>
        <el-form-item label="商品分类">
          <el-select placeholder="请选择商品分类" v-model="formObj.category">
            <el-option
              label="(冒菜)套餐自选区"
              value="(冒菜)套餐自选区"
            ></el-option>
            <el-option label="撸烤串" value="撸烤串"></el-option>
            <el-option label="满减专区" value="满减专区"></el-option>
            <el-option label="营养适配" value="营养适配"></el-option>
            <el-option label="千万销量" value="千万销量"></el-option>
            <el-option label="炒饭系列" value="炒饭系列"></el-option>
            <el-option label="鲜炒盖饭" value="鲜炒盖饭"></el-option>
            <el-option
              label="收藏门店超值专享"
              value="收藏门店超值专享"
            ></el-option>
            <el-option label="热销榜" value="热销榜"></el-option>
            <el-option label="新品专享" value="新品专享"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input-number
            v-model="formObj.price"
            @change="handleChange"
            :min="1"
            :max="10"
            label="描述文字"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="商品图片">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:5000/goods/goods_img_upload"
            :show-file-list="false"
            :on-success="headerSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="商品描述">
          <el-input type="textarea" v-model="formObj.goodsDesc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">添加商品</el-button>
        </el-form-item>
      </el-form>
    </div>
  </com-card>
</template>

<script>
import { api_goodsave } from "../../api/goodType";
import ComCard from "../../components/comCard";

export default {
  components: {
    ComCard,
  },
  data() {
    return {
      num: 1,
      imageUrl: "",
      formObj: {
        name: "",
        category: "",
        price: "",
        imgUrl: "",
        goodsDesc: "",
      },
    };
  },
  methods: {
    //   计数器加减值改变
    handleChange(value) {
      console.log(value);
    },
    headerSuccess(res, file) {
      console.log(res, file);
        this.imageUrl=`http://localhost:5000/upload/imgs/goods_img/${res.imgUrl}`;
          this.formObj.imgUrl=res.imgUrl;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    async save() {
      let rst = await api_goodsave(this.formObj);
      let{code,msg}=rst;
      if(code==0){
              this.$message.success(msg);
              this.$router.replace("/goods/list");
      }
    //   this.imageUrl=`http://localhost:5000/goods/goods_img_upload${res.imgUrl}`
    //   console.log(rst);
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
